package com.shijie.socket;

import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
public class CorsServerEndpointConfig extends ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 设置跨域头（仅握手阶段生效）
        response.getHeaders().put("Access-Control-Allow-Origin", Collections.singletonList("*"));
        response.getHeaders().put("Access-Control-Allow-Credentials", Collections.singletonList("true"));
//        response.getHeaders().put("Access-Control-Allow-Methods", List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
        response.getHeaders().put("Access-Control-Allow-Methods", Collections.singletonList("GET, POST, PUT, DELETE, OPTIONS"));
        response.getHeaders().put("Access-Control-Allow-Headers", Collections.singletonList("*"));
        super.modifyHandshake(sec, request, response);
    }

}